package com.power.sifa.statistics.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.exception.BusinessException;
import com.power.sifa.correction.model.pojo.vo.PhotoVO;
import com.power.sifa.statistics.api.ICorrectionCountFeign;
import com.power.sifa.statistics.model.pageInfo.CameraPageInfo;
import com.power.sifa.statistics.model.pageInfo.CorrectCountPageInfo;
import com.power.sifa.statistics.model.pageInfo.MarkPointPageInfo;
import com.power.sifa.statistics.model.vo.CameraInfoVO;
import com.power.sifa.statistics.model.vo.MarkPointDetailsVO;
import com.power.sifa.statistics.model.vo.MarkPointVO;
import com.power.sifa.statistics.service.CorrectionCountService;
import com.remote.tencent.api.TecentMapFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(value = "CorrectionCountController", tags = {"社区矫正相关统计"})
public class CorrectionCountController implements ICorrectionCountFeign {

    private static final Logger log = LoggerFactory.getLogger(CorrectionCountController.class);
    private final CorrectionCountService correctionCountService;

    @Resource
    private TecentMapFeign tecentMapFeign;

    @Value("${tencent.map.key:#{null}}")
    private String key;

    @Override
    @ApiOperation("查询标记点列表")
    public ResponseDTO<List<MarkPointVO>> markPointList(MarkPointPageInfo pageInfo) {
        Assert.notNull(pageInfo, "请求参数不能为空");
        return ResponseDTO.succData(correctionCountService.markPointList(pageInfo));
    }

    @Override
    @ApiOperation("查询标记点详情")
    public ResponseDTO<MarkPointDetailsVO> markPointDetails(String warningId) {
        Assert.notNull(warningId, "预警信息id不能为空");
        return ResponseDTO.succData(correctionCountService.markPointDetails(warningId));
    }

    /**
     * 统计社矫人员及社矫人员分类
     */
    @Override
    public ResponseDTO countCorrectPersonInfo(List<String> deptIds) {
        return ResponseDTO.succData(correctionCountService.countCorrectPersonInfo(deptIds));
    }

    /**
     * 统计社矫人员罪名
     */
    @Override
    public ResponseDTO countCorrectCrime(List<String> deptIds) {
        return ResponseDTO.succData(correctionCountService.countCorrectCrime(deptIds));
    }

    /**
     * 统计社矫人员区县分布
     */
    @Override
    public ResponseDTO countCorrectPersonLocation(List<String> deptIds) {
        return ResponseDTO.succData(correctionCountService.countCorrectPersonLocation(deptIds));
    }

    /**
     * 按时间查询的告警统计
     *
     * @return
     */
    @Override
    public ResponseDTO countWarningByTime(CorrectCountPageInfo pageInfo) {
        Assert.notNull(pageInfo, "查询条件不能为空");
        return ResponseDTO.succData(correctionCountService.countWarningByTime(pageInfo));
    }

    /**
     * 按条件查询预警详情信息
     *
     * @param pageInfo
     * @return
     */
    @Override
    public ResponseDTO countWarningInfoByCondition(CorrectCountPageInfo pageInfo) {
        Assert.notNull(pageInfo, "查询条件不能为空");
        try {
            PageDataResponse result = correctionCountService.countWarningInfoByCondition(pageInfo);
            return ResponseDTO.succData(result);
        } catch (BusinessException e) {
            return ResponseDTO.wrap(e.getCode(), e.getMsg());
        }

    }

    /**
     * 通过条件查询当前的人员坐标点数据
     *
     * @return
     */
    @Override
    public ResponseDTO getPersonMarkPoint(MarkPointPageInfo pageInfo) {
        Assert.notNull(pageInfo, "查询条件不能为空");
        return ResponseDTO.succData(correctionCountService.getPersonMarkPoint(pageInfo));
    }

    /**
     * 通过公安数据查询当前人员的前后范围的坐标点
     *
     * @param gaId      公安id
     * @param timeScope 时间范围
     * @return
     */
    @Override
    public ResponseDTO getPersonMoveTrack(Integer gaId, Integer timeScope) {
        Assert.notNull(gaId, "公安数据id不能为空");
        Assert.notNull(timeScope, "时间");
        return ResponseDTO.succData(correctionCountService.getPersonMoveTrack(gaId, timeScope));
    }

    @Override
    public ResponseDTO getCameraMarkPoint() {
        return ResponseDTO.succData(correctionCountService.getCameraMarkPoint());
    }

    @Override
    public ResponseDTO getTencentMapAddress(String point) {
        Assert.notNull(point, "坐标点不能为空");
        JSONObject result = tecentMapFeign.reverseParseAddress(key, point);
        if (ObjectUtil.isNotNull(result) && result.containsKey("status") && result.get("status").equals(0)) {
            JSONObject queryResult = result.getObject("result", JSONObject.class);
            if (ObjectUtil.isNotNull(queryResult)) {
                StringBuffer addressString = new StringBuffer();
                if (ObjectUtil.isNotNull(queryResult.get("address_component"))) {
                    Map address_component = (Map) queryResult.getObject("address_component", Map.class);
                    addressString.append(address_component.get("province")).append(address_component.get("city"));
                }
                if (ObjectUtil.isNotNull(queryResult.get("formatted_addresses"))) {
                    Map formatted_addresses = (Map) queryResult.getObject("formatted_addresses", Map.class);
                    addressString.append(formatted_addresses.get("recommend"));
                }
                return ResponseDTO.succData(addressString.toString());
            }

        }
        return ResponseDTO.wrap(500, "坐标点查询地址失败");
    }

    @Override
    public ResponseDTO getCameraInfoDetail(CameraPageInfo cameraPageInfo) {
        Assert.notNull(cameraPageInfo, "查询信息不能为空");
        Assert.notNull(cameraPageInfo.getCameraId(), "摄像头id不能为空");
        CameraInfoVO result = correctionCountService.getCameraInfoDetail(cameraPageInfo);
        if (ObjectUtil.isNotNull(result)) {
            return ResponseDTO.succData(result);
        }
        return ResponseDTO.wrap(500, "查询摄像头详情失败");
    }

    @Override
    public ResponseDTO getMonitorCondition(CorrectCountPageInfo pageInfo) {
        Assert.notNull(pageInfo.getMonitorTime(), "查询日期不能为空");
        try {
            return ResponseDTO.succData(correctionCountService.getMonitorCondition(pageInfo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/crc_count/getMonitorCondition 监控情况获取失败->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "监控情况获取失败");
        }
    }

    @Override
    public ResponseDTO getMonitoringList(CorrectCountPageInfo pageInfo) {
        Assert.notNull(pageInfo.getMonitorTime(), "查询日期不能为空");
        try {
            return ResponseDTO.succData(correctionCountService.getMonitoringList(pageInfo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/crc_count/getMonitoringList 监控列表情况获取失败->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "监控列表情况获取失败");
        }
    }

    public ResponseDTO  getWarningPhotoMap(Integer gaId, Integer timeScope) {
        Assert.notNull(gaId, "公安ID不能为空");
        try {
            return ResponseDTO.succData(correctionCountService.getWarningPhotoMap(gaId, timeScope));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/crc_count/getWarningPhotoMap 监控图片获取失败->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "监控图片获取失败");
        }
    }


}
